package com.ls.lserp.modules.material.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ls.lserp.dbaccess.Entity.SampleEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.common.entity.OgnizationEntity;
import com.ls.lserp.modules.material.dao.SlopworkOutrequestDao;
import com.ls.lserp.modules.material.entity.InventoryRequest;
import com.ls.lserp.modules.material.entity.InventoryRequestEx;
import com.ls.lserp.modules.material.entity.SlopworkInRequest;
import com.ls.lserp.modules.material.entity.SlopworkOutrequest;

@Service
@Transactional(readOnly = false)
public class SlopworkOutrequestService  extends CrudService<SlopworkOutrequestDao,SlopworkOutrequest>{
	
	 @Autowired
	 private SlopworkOutrequestDao slopworkOutrequestDao;
	 
	 /**
		 * @Title: findList
		 * @Description: 一览查询
		 * @param page
		 * @param orderMateriel
		 * @return Page<OrderMateriel>
		 */
		public Page<SlopworkOutrequest> findList(Page<SlopworkOutrequest> page, SlopworkOutrequest slopworkOutrequest) {
			
			List<SlopworkOutrequest> irDetailList = null;
			BigDecimal tempRequestQuantity = null;
			BigDecimal tempRealQuantity = null;
			BigDecimal tempResidueQuantity = null;
			
			// tab页分，检索条件设定(0:全部,1:待入库,2:未全部入库,3:全部入库)
			slopworkOutrequest.setOutboundStatus(ResourcesUtil.getConfig("outbound_status_wait"));
			
			// 入出库区分（出库)
			slopworkOutrequest.setOutboundInKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_out"));
			
			// 设置分页参数
			slopworkOutrequest.setPage(page);
			List<SlopworkOutrequest> resultList = slopworkOutrequestDao.findList(slopworkOutrequest);
			
			if(!CollectionsUtils.isEmpty(resultList)){
				// 库存申请信息
				for(SlopworkOutrequest ir : resultList){
					tempRequestQuantity = new BigDecimal("0");
					tempRealQuantity = new BigDecimal("0");
					tempResidueQuantity = new BigDecimal("0");
					irDetailList = slopworkOutrequestDao.findClothesRequestDetailList(ir);
					if(!CollectionsUtils.isEmpty(irDetailList)){
						// 库存申请明细
						for(SlopworkOutrequest ird : irDetailList){
							tempRequestQuantity = tempRequestQuantity.add(checkBigDecimalNull(ird.getRequestOutQuantity()));
							tempRealQuantity = tempRealQuantity.add(checkBigDecimalNull(ird.getRealOutQuantity()));
						}
						tempResidueQuantity = tempRequestQuantity.subtract(tempRealQuantity);
						ir.setRequestOutQuantity(tempRequestQuantity);
						ir.setRealOutQuantity(tempRealQuantity);
						ir.setResidueOutQuantity(tempResidueQuantity);
					}
				}
			}
			
	        // 执行分页查询
	        page.setList(resultList);
	        
	        return page;
		}
		
		/**
		 * check BigDecimal
		 * @param cost
		 * @return 取不到返回0
		 */
		public BigDecimal checkBigDecimalNull(BigDecimal cost){
			if(cost==null){
				return new BigDecimal("0");
			}
			return cost;
		}

		/**
		 * @Title: findShowRequestDetailList
		 * @Description: 入库申请明细
		 * @param @param slopworkInRequest
		 * @param @return
		 * @return List<SlopworkInRequest>
		 * @throws
		 */
		public List<SlopworkOutrequest> findShowRequestDetailList(SlopworkOutrequest slopworkOutrequest) {
            List<SlopworkOutrequest>  dataList = new ArrayList<>();
            //样品入库
           if(ResourcesUtil.getConfig ("clothes_out_category_Samply").equals(slopworkOutrequest.getOutboundCategory())) {
               dataList = slopworkOutrequestDao.findShowSamplyRequestDetailList(slopworkOutrequest);
			   if(!CollectionsUtils.isEmpty(dataList)&& dataList.size() !=0){
				   slopworkOutrequest.setCustomerContractNo(dataList.get(0).getCustomerContractNo());
			   }
           }else{
               dataList = slopworkOutrequestDao.findShowRequestDetailList(slopworkOutrequest);
           }
		   return dataList;
		}
		
		 /**
	     * @Title: getProductionList
	     * @Description: 查询
	     * @param @return
	     * @return List<SlopworkOutrequest> 
	     * @throws
	     */
	   public List<SlopworkOutrequest> getProductionList() {
	       
	       return slopworkOutrequestDao.getProductionList();
	   }
	   
	   /**
	    * @Title: getFabricatList
	    * @Description: 查询
	    * @param @return
	    * @return List<ognization> 
	    * @throws
	    */
	  public List<OgnizationEntity> getFabricatList() {
	      
	      return slopworkOutrequestDao.getFabricatList();
	  }
	  
	  /**
	   * @Title: getContractProcessingList
	   * @Description: 查询
	   * @param @param slopworkOutrequest
	   * @param @return
	   * @return List<SlopworkOutrequest> 
	   * @throws
	   */
	 public List<SlopworkOutrequest> getContractProcessingList(SlopworkOutrequest slopworkOutrequest) {
	     
	     return slopworkOutrequestDao.getContractProcessingList(slopworkOutrequest);
	 }
	 
	 /**
	  * @Title: getStyleList
	  * @Description: 查询
	  * @param @param slopworkOutrequest
	  * @param @return
	  * @return List<ognization> 
	  * @throws
	  */
	public List<SlopworkOutrequest> getStyleList(SlopworkOutrequest slopworkOutrequest) {
	    
	    return slopworkOutrequestDao.getStyleList(slopworkOutrequest);
	}

	/**
	 * @Title: findCreateRequestDetailList
	 * @Description: 入库申请明细
	 * @param @param slopworkOutrequest
	 * @param @return
	 * @return List<SlopworkOutrequest>
	 * @throws
	 */
	public List<SlopworkOutrequest> findCreateRequestDetailList(SlopworkOutrequest slopworkOutrequest) {
		
		return slopworkOutrequestDao.findCreateRequestDetailList(slopworkOutrequest);
	}

	/**
	 * @Title: getProduction
	 * @Description: 查询
	 * @param @param getProduction
	 * @param @return
	 * @return SlopworkOutrequest
	 * @throws
	 */
	public SlopworkOutrequest getProduction(SlopworkOutrequest slopworkOutrequest) {
		
		return slopworkOutrequestDao.getProduction(slopworkOutrequest);
	}

	/**
	 * @Title: getContractNo
	 * @Description: 查询客户款号和客户合同号
	 * @param @param slopworkInRequest
	 * @param @return
	 * @return SlopworkInRequest
	 * @throws
	 */
	public SlopworkOutrequest getContractNo(SlopworkOutrequest slopworkOutrequest) {

		return slopworkOutrequestDao.getContractNo(slopworkOutrequest); 
	}
	
	/**
	 * @Title: saveInventoryRequest
	 * @Description: 保存入库申请
	 * @param @param inventoryRequest
	 * @return void 
	 * @throws
	 */
	@Transactional(readOnly = false)
	public void saveInventoryRequest(SlopworkOutrequest slopworkOutreques) {
		
		// 判断新建编辑
		//若为null 执行插入语句（新建）
		if (slopworkOutreques.getId()==null){			
			slopworkOutreques.preInsert();
			slopworkOutreques.setVersion(1L);
			slopworkOutreques.setDeleteFlag(InventoryRequest.DEL_FLAG_NORMAL);
			slopworkOutreques.setEnableFlag(InventoryRequest.ENABlE_FLAG_NORMAL);
			slopworkOutreques.setOutboundStatus(ResourcesUtil.getConfig("outbound_status_wait"));
			slopworkOutreques.setOutboundInKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_out"));
            slopworkOutrequestDao.insertInventoryRequest(slopworkOutreques);
		}else{
			// 更新数据
			slopworkOutreques.preUpdate();
			slopworkOutrequestDao.updateInventoryRequest(slopworkOutreques);
			// 删除入库单明细
			slopworkOutrequestDao.deleteRequestDetails(slopworkOutreques);
		}
		
		
		// 判断list是否为空
		if(!CollectionsUtils.isEmpty(slopworkOutreques.getRequestOutDetailsList())){
		   // 遍历赋值
	       for(SlopworkOutrequest itr : slopworkOutreques.getRequestOutDetailsList()){
	      	 itr.setId(slopworkOutreques.getId());
	      	 itr.preInsert();
	      	 itr.setVersion(1L);
	      	 itr.setDeleteFlag(InventoryRequestEx.DEL_FLAG_NORMAL);
	      	 itr.setEnableFlag(InventoryRequestEx.ENABlE_FLAG_NORMAL);
	      	 itr.setStockinStatus(ResourcesUtil.getConfig("outbound_status_wait"));
			   //如果入库申请的数量为空，不插入数据
			  if(!"".equals(itr.getRequestOutQuantity())&& null !=itr.getRequestOutQuantity()){
				  slopworkOutrequestDao.insertInventoryRequestDetails(itr);
			  }

			}
		}
	}
	/**
	 * @Title: getFabricatList
	 * @Description: 查询样品订单
	 * @param @return
	 * @return List<ognization>
	 * @throws
	 */
	public List<SampleEntity> getSamplyOrderList() {

		return slopworkOutrequestDao.getSamplyOrderList();
	}

	/**
	 * @Title: findCreateSamplyRequestDetailList
	 * @Description: 样品入库申请明细
	 * @param @param slopworkOutrequest
	 * @param @return
	 * @return List<SlopworkOutrequest>
	 * @throws
	 */
	public List<SlopworkOutrequest> findCreateSamplyRequestDetailList(SlopworkOutrequest slopworkOutrequest) {

		return slopworkOutrequestDao.findSamplyClothesRequestDetailList(slopworkOutrequest);
	}
	/**
	 * @Title: getContractList
	 * @Description: 查询
	 * @param @return
	 * @return List<SlopworkOutrequest>
	 * @throws
	 */
	public List<SlopworkOutrequest> getContractList() {

		return slopworkOutrequestDao.getContractList();
	}
	/**
	 * @Title: getContractStyleList
	 * @Description: 查询
	 * @param @param slopworkOutrequest
	 * @param @return
	 * @return List<ognization>
	 * @throws
	 */
	public List<SlopworkOutrequest> getContractStyleList(SlopworkOutrequest slopworkOutrequest) {

		return slopworkOutrequestDao.getContractStyleList(slopworkOutrequest);
	}
	/**
	 * @Title: findCreateContractList
	 * @Description: 查询
	 * @param @return
	 * @return List<SlopworkOutrequest>
	 * @throws
	 */
	public List<SlopworkOutrequest> findCreateContractList(SlopworkOutrequest slopworkOutrequest) {

		return slopworkOutrequestDao.findCreateContractList(slopworkOutrequest);
	}
	/**
	 * @Title: findCreateContractList
	 * @Description: 查询
	 * @param @return
	 * @return List<SlopworkOutrequest>
	 * @throws
	 */
	public List<SlopworkOutrequest>   getStyleNoByStyleId(Long styleId) {

		return slopworkOutrequestDao.getStyleNoByStyleId(styleId);
	}
}
